using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Dapr;
using Furion.FriendlyException;
using Kevin.Dapr.Infrastructure;

namespace Kevin.Dapr.OrderService.Controllers
{
    public class DaprHttpService : IDynamicApiController
    {
        /// <summary>
        /// dapr 抛异常
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<DaprStructValue<string>> ThrowException()
        {
            throw new Exception("您好，代码出错了");
        }

        /// <summary>
        /// dapr get无参
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<DaprStructValue<string>> GetByNoParameters()
        {
            var data = "您在" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "访问了OrderService的Action：GetByNoParameters";
            return Task.FromResult(new DaprStructValue<string> { Value = data });
        }

        /// <summary>
        /// dapr get有参
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<List<string>> GetByParameters(int page, int size)
        {
            if (page < 1 || size < 1)
                throw Oops.Bah("页码错误");
            var db = new List<string>();
            for (int i = 0; i < 100; i++)
            {
                db.Add($"我的订单{i.ToString()}");
            }
            var data = db.Skip(page * size).Take(size).ToList();
            return Task.FromResult(data);
        }

        /// <summary>
        /// post 带参数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Task<AddOrderInput> PostByParameters(AddOrderInput input)
        {
            return Task.FromResult(new AddOrderInput(input.Id + 1, input.Address + "a", input.number + 1));
        }

        /// <summary>
        /// 发布订阅
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        [Topic("pubsub", "user_topic")]
        public async Task Subscription(SendMessageInput message)
        {
            Console.WriteLine($"促发发送短信1---手机号：{message.cellphone}---短信内容：{message.content}");
            await Task.CompletedTask;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="Address"></param>
    /// <param name="number"></param>
    public record AddOrderInput(long Id, string Address, int number);


    public record SendMessageInput(string cellphone, string content);
}
